<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UsersTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveUserData($users)
    {
		$status="";
		if($users['admin_id']==0){
			$status=0;
		}else{
			$status=1;
		}
		$password=md5($users['user_password']);
		$data = array(
			'user_name' 	  	       => $users['user_name'],		
			'user_email' 		       => $users['user_email'],  		
			'user_password' 		   => $password,		
			'user_mobile' 		       => $users['user_mobile'],				
			'user_type_id' 		       => $users['user_type_id'],				
			'status'  	               => $status,  				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }	
	// update
	public function updateUserData($users)
    {
		$password=md5($users['user_password']);
		$data = array(
			'user_name' 	  	       => $users['user_name'],		
			'user_email' 		       => $users['user_email'],  		
			'user_password' 		   => $password,		
			'user_mobile' 		       => $users['user_mobile'],				
			'user_type_id' 		       => $users['user_type_id'],				
			'status'  	               => 1,  				
		);	
		 $updateuserid=$this->tableGateway->update($data, array('user_email' => $users['user_email']));
	   return $updateuserid; 	
    }
	
	public function userStatusUpdate($userid,$status){
	   $data = array(
			'status'          =>'1',
	   );
	   $updateuserid=$this->tableGateway->update($data, array('user_id' => $userid));
	   return $updateuserid;                        
	}
	public function checkloginEmail($data){  
		$select = $this->tableGateway->getSql()->select();                        
	    $select->where('user_email="'.$data['user_email'].'"');                                         
	    $select->where('user_password="'.md5($data['user_password']).'"');
	    $select->where('user_type_id="'.$data['user_type_id'].'"');
	    $select->where('status="1"');                                         
		$resultSet = $this->tableGateway->selectWith($select); 
	    return $resultSet;
	}
	public function checkUniqueRecord($email){        
		$select = $this->tableGateway->getSql()->select()                        
			   ->where('user_email="'.$email.'"');                                         
	   $resultSet = $this->tableGateway->selectWith($select);                            
	   return $resultSet;
	}
	public function changepwd($userid,$pwd){
	   $password=md5($pwd);
	   $data = array(
		   'user_password'  => $password,
		);
	   $changepassword=$this->tableGateway->update($data, array('user_id' => $userid));	   
	   return $changepassword;                        
	}
	
	public function updateUserRegAuth($userid){
	   $data = array(
			'status'          =>'1',
	   );
	   $updateuserid=$this->tableGateway->update($data, array('user_id' => $userid));
	   return $updateuserid;                        
	}
	public function userdetails($id)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_id="'.$id.'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();
	}
	public function getUserData($user_id)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_id="'.$user_id.'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function checkpassword($adminId,$data)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_id="'.$adminId.'"');	
		$select->where('user_password="'.md5($data['old_password']).'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function updateOldpassword($admin_id,$newpassword){
	   $data = array(
			'user_password'          =>md5($newpassword),
	   );
	   $updateuserid=$this->tableGateway->update($data, array('user_id' => $admin_id));
	   return $updateuserid;                        
	}
	public function getUserlist()
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('status="1"');	
		$select->where('user_type_id="2"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getAdminlist()
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('status="1"');	
		$select->where('user_type_id = 1');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function deleteUser($user_id)
    {	
	    $deleteStatus=$this->tableGateway->delete(array('user_id' => $user_id));
        return $deleteStatus;
	}
}